package cn.lbd.arithmetic.leetcode.editor.cn;
//给定一个二叉树，判断它是否是高度平衡的二叉树。
//
// 本题中，一棵高度平衡二叉树定义为： 
//
// 
// 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。 
// 
//
// 示例 1: 
//
// 给定二叉树 [3,9,20,null,null,15,7] 
//
//     3
//   / \
//  9  20
//    /  \
//   15   7 
//
// 返回 true 。 
// 
//示例 2: 
//
// 给定二叉树 [1,2,2,3,3,null,null,4,4] 
//
//        1
//      / \
//     2   2
//    / \
//   3   3
//  / \
// 4   4
// 
//
// 返回 false 。 
//
// 
// Related Topics 树 深度优先搜索 
// 👍 480 👎 0


//leetcode submit region begin(Prohibit modification and deletion)


/*class Solution110 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode heroTreeNode2 = new TreeNode(2);
        TreeNode heroTreeNode3 = new TreeNode(3);
        TreeNode heroTreeNode4 = new TreeNode(4);
        TreeNode heroTreeNode5 = new TreeNode(5);
        TreeNode heroTreeNode6 = new TreeNode(6);

        *//*TreeNode root = new TreeNode(3);
        TreeNode heroTreeNode9 = new TreeNode(9);
        TreeNode heroTreeNode20 = new TreeNode(20);
        TreeNode heroTreeNode15 = new TreeNode(15);
        TreeNode heroTreeNode7 = new TreeNode(7);*//*

        *//*root.setLeft(heroTreeNode9);
        root.setRight(heroTreeNode20);
        heroTreeNode20.setLeft(heroTreeNode15);
        heroTreeNode20.setRight(heroTreeNode7);*//*

        root.setLeft(heroTreeNode2);
        root.setRight(heroTreeNode3);
        heroTreeNode2.setLeft(heroTreeNode4);
        heroTreeNode2.setRight(heroTreeNode5);
        heroTreeNode5.setLeft(heroTreeNode6);

        System.out.println(isBalanced(root));
    }

    public static boolean isBalanced(TreeNode root) {
        return root.infixOrder();
    }

}

class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    TreeNode(int x) {
        val = x;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "val=" + val +
                '}';
    }

    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }

    public boolean infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        if (Math.abs(this.leftHeight() - this.rightHeight()) > 1) {
            return false;
        }
        if (this.right != null) {
            this.right.infixOrder();
        }
        return true;
    }
}*/
//leetcode submit region end(Prohibit modification and deletion)
